<?php

namespace App\Http\Controllers;

use App\Models\Member;
use App\Models\TiCash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class MemberController extends BaseController
{
    /**
     * 获取用户信息
     */
    public function getUserInfo()
    {
        $input = Input::all();
        $session = $this->getSessionBy3rd($input['_3rd_session']);
        $member_info = Member::select('id','name','avatar','sex','account','bi','pid','child_nums','check_day','created_at')
            ->where('openid', $session['openid'])
            ->first();

        return $this->success('成功', $member_info);
    }

    //获取好友列表
    public function getChildren()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        try{
            $list = Member::where('pid', $member_info['id'])->get();
        }catch (\Exception $e) {
            $list = [];
        }

        return $this->success('成功', $list);
    }

    //后去当日收益
    public function getSy()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        $member_sy = $this->getTodaySy($member_info['id']);
        if(isset($member_sy[0])){
            $result = $member_sy[0];
        }else{
            $result['money'] = 0;
            $result['money_cash'] = 0;
        }
        $result['bi'] = $member_info['bi'];
        $result['account'] = $member_info['account'];

        return $this->success('成功', $result);
    }

    //申请提现
    public function applyCash()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        if(isset($input['money']) && is_numeric($input['money']) && $input['money']>0) {
            $types = isset($input['types']) ? $input['types'] : 1;
            if($types) {  //1是现金提现
                DB::beginTransaction();
                $member = Member::where('id',$member_info['id'])
                    ->lockForUpdate()
                    ->first();
                if($input['money'] > $member->account) {
                    DB::rollBack();

                    return $this->error('提现金额不够!');
                }
                $ticash_insert = [
                    'member_id' => $member->id,
                    'staus' => 0,
                    'type' => 1,
                    'money' => $input['money'],
                    'bei' => isset($input['bei']) ? $input['bei'] : ''
                ];
                $res_ticash = TiCash::create($ticash_insert);
                $res_mmeber = $member->decrement('account', $input['money']);
                if($res_mmeber && $res_ticash) {
                    DB::commit();
                    return $this->success('成功');
                }else{
                    DB::rollBack();
                    return $this->error('提现错误');
                }
            }else{
                DB::beginTransaction();
                $member = Member::where('id',$member_info['id'])
                    ->lockForUpdate()
                    ->first();
                if($input['money'] > $member->bi) {
                    DB::rollBack();

                    return $this->error('提现金额不够!');
                }
                $ticash_insert = [
                    'member_id' => $member->id,
                    'staus' => 0,
                    'type' => 0,
                    'money' => $input['money'],
                    'bei' => isset($input['bei']) ? $input['bei'] : ''
                ];
                $res_ticash = TiCash::create($ticash_insert);
                $res_mmeber = $member->decrement('bi', $input['money']);
                if($res_mmeber && $res_ticash) {
                    DB::commit();
                    return $this->success('成功');
                }else{
                    DB::rollBack();
                    return $this->error('提现错误');
                }
            }
        }
    }

    //获取提现记录
    public function getTiCash()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        $page = isset($input['page']) ? $input['page'] : 1;
        $pagesize = isset($input['pagesize']) ? $input['pagesize'] : 10;
        $pagevar = ($page-1)*$pagesize;
        $types = isset($input['types']) ? $input['types'] : 1;
        $model = TiCash::select('member_id','status','money','bei','created_at','not_pass_reason');
        $list = $model
            ->where('member_id', $member_info['id'])
           // ->where('type', $types)
            ->orderby('created_at', 'desc')
            ->skip($pagevar)
            ->take($pagesize)
            ->get();
        foreach ($list as $k=>$v) {
            $v->status_str = (new TiCash())->status_arr[$v->status];
        }

        return $this->success('成功', $list);
    }


}
